import jsYaml, { Type } from "js-yaml";
//组成成分
export type Item = {
  // 材料类型ID
  materialTypeID: number;

  // 材料数量
  quantity: number;
};

// 定义表示类型材料的类型
export type YAMLTypeMaterials = {
  // 材料组成
  materials: Item[];
};

export type YAMLTypeMaterialsMap = Record<number, YAMLTypeMaterials>;
export const YAMLTypeMaterialsSchema = jsYaml.DEFAULT_SCHEMA.extend([
  new Type("!YAMLTypeMaterialsMap", {
    kind: "mapping",
    construct: (data: YAMLTypeMaterials) => {
      // 将适当字段解析为数字
      data.materials = data.materials.map((item) => {
        return {
          materialTypeID: Number(item.materialTypeID),
          quantity: Number(item.quantity),
        };
      });
      return data;
    },
    instanceOf: Object,
    represent: (data: any) => data,
  }),
]);
